package com.djq.hhc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.djq.hhc.dto.HomeFamilyDto;
import com.djq.hhc.dto.PostTypeDto;
import com.djq.hhc.model.home.HomeFamily;
import com.djq.hhc.model.post.PostType;
import com.djq.hhc.result.Result;
import com.djq.hhc.service.HomeFamilyService;
import com.djq.hhc.vo.HomeFamilyVo;
import com.djq.hhc.vo.PostTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: dadd7
 * @Date: 2023/03/05/下午 4:59
 * @Description:
 */

@RestController
@RequestMapping(value = "/home/homeFamily")
public class HomeFamilyController {

    @Autowired
    private HomeFamilyService homeFamilyService;

    //返回所有family, 不包含时间
    @GetMapping("/getAllFamily")
    public Result getAllFamily() {
        List<HomeFamily> list = homeFamilyService.list();
        List<HomeFamilyDto> res = list.stream()
                .map(homeFamily -> new HomeFamilyDto(homeFamily.getId(), homeFamily.getFamilyName()))
                .collect(Collectors.toList());
        return Result.ok(res);
    }

    //新增
    @PostMapping("/save")
    public Result save(@RequestBody HomeFamily homeFamily) {
        return homeFamilyService.saveFamily(homeFamily);
    }

    //修改
    @PutMapping("/update")
    public Result updateType(@RequestBody HomeFamily homeFamily) {
        return homeFamilyService.updateFamily(homeFamily);
    }

    //删除
    @DeleteMapping("/remove/{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean b = homeFamilyService.removeById(id);
        if (b) {
            return Result.ok().message("删除成功!");
        } else {
            return Result.fail().message("删除失败");
        }
    }

    //获取
    @GetMapping("/get/{id}")
    public Result getTypeById(@PathVariable Integer id) {
        HomeFamily homeFamily = homeFamilyService.getById(id);
        return Result.ok(homeFamily);
    }

    //分页
    @PostMapping("{page}/{limit}")
    public Result index(
            @PathVariable Long page,
            @PathVariable Long limit,
            @RequestBody(required = false) HomeFamilyVo homeFamilyVo) {
        //创建page对象，传递当前页和每页记录数
        Page<HomeFamily> pageParam = new Page<>(page, limit);
        //获取条件值
        Long id = homeFamilyVo.getId();
        String familyName = homeFamilyVo.getFamilyName();
        //封装条件
        QueryWrapper<HomeFamily> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(id)) {
            wrapper.like("id",id);
        }
        if(!StringUtils.isEmpty(familyName)) {
            wrapper.like("family_name",familyName);
        }
        //调用方法得到分页查询结果
        IPage<HomeFamily> pageModel = homeFamilyService.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }
}
